/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taganalysistoolsclient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 *
 * @author lendle
 */
public class RandomCategorySelection {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                new FileInputStream(
                new File("applications\\delicious\\computer.category")),
                "utf-8"));
        List<String> list = new ArrayList<String>();
        Collections.sort(list);
        String str = reader.readLine();
        while (str != null) {
            list.add(str);

            str = reader.readLine();
        }
        reader.close();

        //the selection process
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(
                new FileOutputStream("applications\\delicious\\computer.category.selected"),
                "utf-8"));
        Random random = new Random();
        boolean first = true;
        int count = 0;
        List<String> resultList=new ArrayList<String>();
        while (count < 200) {
            int selectedIndex = random.nextInt(list.size());
            String _str = list.remove(selectedIndex);
            resultList.add(_str);
            count++;
            int groupSeparator=_str.lastIndexOf("/");
            String group=_str.substring(0, groupSeparator);
            for (int j = selectedIndex + 1; count < 200 && j < list.size(); j++) {
                String relatedStr = list.get(j);
                if (relatedStr.startsWith(group)) {
                    System.out.println("related: "+relatedStr+", "+_str);
                    count++;
                    list.remove(j);
                    resultList.add(relatedStr);
                }
                else{
                    break;
                }
            }
        }
        Collections.sort(resultList);
        for(String _str : resultList){
            System.out.println(_str);
            if(!first){
                writer.println("");
            }
            first=false;
            writer.print(_str);
        }
        writer.close();
    }
}
